use std::collections::HashMap;
use std::fs;

// 解析VIVO测试目录并生成Rust项目结构
pub fn _traverse_vivo_dir(
    dir_path: &str,
    test_path: &str,
    filenames: Vec<String>,
    header_files: &mut HashMap<String, String>,
    source_files: &mut HashMap<String, String>,
    test_files: &mut HashMap<String, String>,
) -> std::io::Result<()> {
    let dir = fs::read_dir(dir_path)?;
    for entry in dir {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() {
            //println!("File name:{:?}",path.file_name().unwrap());
            if let Some(extension) = path.extension() {
                if extension == "h" {
                    // 读取头文件内容
                    if let Ok(content) = fs::read_to_string(&path) {
                        let filename = path.file_stem().unwrap().to_str().unwrap().to_string();
                        if filenames.contains(&filename) {
                            header_files.insert(filename.clone(), content);
                        }
                    }
                } else if extension == "c" {
                    // 读取C源文件内容
                    if let Ok(content) = fs::read_to_string(&path) {
                        let filename = path.file_stem().unwrap().to_str().unwrap().to_string();
                        if filenames.contains(&filename) {
                            source_files.insert(filename.clone(), content);
                        }
                    }
                }
            }
        } else if path.is_dir() {
            let _ = _traverse_vivo_dir(
                &path.to_string_lossy(),
                test_path,
                filenames.clone(),
                header_files,
                source_files,
                test_files,
            )
            .unwrap();
        }
    }

    let test_dir = fs::read_dir(test_path)?;
    let test_names = filenames
        .iter()
        .map(|it| format!("test-{}", it))
        .collect::<Vec<String>>();
    for entry in test_dir {
        let entry = entry?;
        let path = entry.path();
        if path.is_file() {
            //println!("File name:{:?}",path.file_name().unwrap());
            if let Ok(content) = fs::read_to_string(&path) {
                let filename = path.file_stem().unwrap().to_str().unwrap().to_string();
                if test_names.contains(&filename) {
                    test_files.insert(filename.clone(), content);
                }
            }
        } else if path.is_dir() {
            let _ = _traverse_vivo_dir(
                &path.to_string_lossy(),
                test_path,
                filenames.clone(),
                header_files,
                source_files,
                test_files,
            )
            .unwrap();
        }
    }

    Ok(())
}

// 解析普通项目目录并生成Rust项目结构
pub fn _parse_project_dir(_project_dir: &str) -> Result<(), Box<dyn std::error::Error>> {
    // TODO: 解析普通项目目录并生成Rust项目结构
    Ok(())
}
